package com.lanou.musicproject.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.MusicResult;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;


/**
 * Created by dllo on 15/10/22.
 * 音乐馆 gridview适配器
 */
public class RightAllGridAdapter extends BaseAdapter {
    private ArrayList<MusicResult> datas;
    private Context context;
    private MusicResult result;

    public RightAllGridAdapter(ArrayList<MusicResult> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView == null) {
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_rightall_grid, null);
            holder.cardView = (CardView) convertView.findViewById(R.id.item_rightall_grid_cardview);
            holder.numTv = (TextView) convertView.findViewById(R.id.item_rightall_grid_cardview_tv);
            holder.nameTv = (TextView) convertView.findViewById(R.id.item_rightall_gird_tv);
            holder.iv = (ImageView) convertView.findViewById(R.id.item_rightall_gird_iv);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        result = datas.get(position);
        holder.nameTv.setText(result.getSinger_name());
        String url = result.getPic_url();
        if (url != null) {
            Picasso.with(context).load(url).into(holder.iv);
        } else {
            holder.iv.setImageResource(R.mipmap.ic_launcher);
        }

        return convertView;
    }

    private class MyViewHolder {
        private CardView cardView;
        private TextView numTv;
        private TextView nameTv;
        private ImageView iv;
    }
}
